const db = require('../db/index')
// 获取机长列表
exports.numberList = (req, res) => {
  let pageData = req.body
  let page = pageData.page * 1 - 1
  let size = pageData.size * 1
  let from = page * size
  let sql = `select * from numberList  limit ${from},${from + size}`
  let sql2 = `select count(*) from numberList `
  db.query(sql, [pageData.appid], (err, res2) => {
    if (err) return res.cc(err)
    db.query(sql2, [], (err, res3) => {
      res.send({
        res: res2,
        total: res3[0]['count(*)']
      })
    })

  })
}
exports.numberAdd = (req, res) => {
  let pageData = req.body
  let sql = `insert into numberList set ?`
  db.query(sql, [pageData], (err, res2) => {
    if (err) return res.cc(err)
    res.send({
      message: '添加成功'
    })
  })
}
// 删除
exports.numberDelete = (req, res) => {
  let pageData = req.body.id
  let sql = `delete from numberList where _id=?`
  db.query(sql, [pageData], (err, res2) => {
    if (err) return res.cc(err)
    res.send({
      message: '删除成功'
    })
  })
}
// 修改
exports.numberChange = (req, res) => {

  let data = req.body
  let id = data._id
  delete data._id
  let sql = `update numberList set ? where _id=? `
  db.query(sql, [data, id], (err, res2) => {
    if (err) return res.cc(err)
    res.send({
      message: '删除成功'
    })
  })
}

// 条件查询
exports.numberSerach = (req, res) => {
  let pageData = req.body
  let page = pageData.page * 1 - 1
  let size = pageData.size * 1
  let from = page * size
  pageData.code = pageData.code ? pageData.code : ''
  pageData.name = pageData.name ? pageData.name : ''
  pageData.phone = pageData.phone ? pageData.phone : ''
  const sql = `select * from numberList where (code like "%${pageData.code}%" and name like "%${pageData.name}%" and phone like "%${pageData.phone}%")  limit ${size} OFFSET  ${from}`
  db.query(sql, [], (err, res2) => {
    if (err) return res.cc(err)
    let sql2 = `select count(*) from numberList where (code like "%${pageData.code}%" and name like "%${pageData.name}%" and phone like "%${pageData.phone}%")`
    db.query(sql2, [], (err2, res3) => {
      if (err2) return res.cc(err2)
      res.send({
        res: res2,
        total: res3[0]['count(*)']
      })
    })
  })
}




// 获取品检列表
exports.number2List = (req, res) => {
  let pageData = req.body
  let page = pageData.page * 1 - 1
  let size = pageData.size * 1
  let from = page * size
  let sql = `select * from numberList2  limit ${from},${from + size}`
  let sql2 = `select count(*) from numberList2 `
  db.query(sql, [pageData.appid], (err, res2) => {
    if (err) return res.cc(err)
    db.query(sql2, [], (err, res3) => {
      res.send({
        res: res2,
        total: res3[0]['count(*)']
      })
    })

  })
}
exports.number2Add = (req, res) => {
  let pageData = req.body
  let sql = `insert into numberList2 set ?`
  db.query(sql, [pageData], (err, res2) => {
    if (err) return res.cc(err)
    res.send({
      message: '添加成功'
    })
  })
}
// 删除
exports.number2Delete = (req, res) => {
  let pageData = req.body.id
  let sql = `delete from numberList2 where _id=?`
  db.query(sql, [pageData], (err, res2) => {
    if (err) return res.cc(err)
    res.send({
      message: '删除成功'
    })
  })
}
// 修改
exports.number2Change = (req, res) => {

  let data = req.body
  let id = data._id
  delete data._id
  let sql = `update numberList2 set ? where _id=? `
  db.query(sql, [data, id], (err, res2) => {
    if (err) return res.cc(err)
    res.send({
      message: '删除成功'
    })
  })
}

// 条件查询
exports.number2Serach = (req, res) => {
  let pageData = req.body
  let page = pageData.page * 1 - 1
  let size = pageData.size * 1
  let from = page * size
  pageData.code = pageData.code ? pageData.code : ''
  const sql = `select * from numberList2 where (code like "%${pageData.code}%")  limit ${size} OFFSET  ${from}`
  db.query(sql, [], (err, res2) => {
    if (err) return res.cc(err)
    let sql2 = `select count(*) from numberList2 where (code like "%${pageData.code}%" )`
    db.query(sql2, [], (err2, res3) => {
      if (err2) return res.cc(err2)
      res.send({
        res: res2,
        total: res3[0]['count(*)']
      })
    })
  })
}






// 获取大卷列表
exports.number3List = (req, res) => {
  let pageData = req.body
  let page = pageData.page * 1 - 1
  let size = pageData.size * 1
  let from = page * size
  let sql = `select * from numberList3  limit ${from},${from + size}`
  let sql2 = `select count(*) from numberList3 `
  db.query(sql, [pageData.appid], (err, res2) => {
    if (err) return res.cc(err)
    db.query(sql2, [], (err, res3) => {
      res.send({
        res: res2,
        total: res3[0]['count(*)']
      })
    })

  })
}
exports.number3Add = (req, res) => {
  let pageData = req.body
  let sql = `insert into numberList3 set ?`
  db.query(sql, [pageData], (err, res2) => {
    if (err) return res.cc(err)
    res.send({
      message: '添加成功'
    })
  })
}
// 删除
exports.number3Delete = (req, res) => {
  let pageData = req.body.id
  let sql = `delete from numberList3 where _id=?`
  db.query(sql, [pageData], (err, res2) => {
    if (err) return res.cc(err)
    res.send({
      message: '删除成功'
    })
  })
}
// 修改
exports.number3Change = (req, res) => {

  let data = req.body
  let id = data._id
  delete data._id
  let sql = `update numberList3 set ? where _id=? `
  db.query(sql, [data, id], (err, res2) => {
    if (err) return res.cc(err)
    res.send({
      message: '删除成功'
    })
  })
}

// 条件查询
exports.number3Serach = (req, res) => {
  let pageData = req.body
  let page = pageData.page * 1 - 1
  let size = pageData.size * 1
  let from = page * size
  pageData.code = pageData.code ? pageData.code : ''
  const sql = `select * from numberList3 where (code like "%${pageData.code}%")  limit ${size} OFFSET  ${from}`
  db.query(sql, [], (err, res2) => {
    if (err) return res.cc(err)
    let sql2 = `select count(*) from numberList3 where (code like "%${pageData.code}%" )`
    db.query(sql2, [], (err2, res3) => {
      if (err2) return res.cc(err2)
      res.send({
        res: res2,
        total: res3[0]['count(*)']
      })
    })
  })
}